package tasks.leetcode;

import java.util.ArrayList;
import java.util.List;

public class TaskIntersection {
    public static void main(String[] args) {
        TaskIntersection taskIntersection = new TaskIntersection();
        int[] intersection = taskIntersection
                .intersection(new int[]{4, 9, 5}, new int[]{9, 4, 9, 8, 4});
        System.out.println(intersection);
    }
    public int[] intersection(int[] nums1, int[] nums2) {
        // 极端场景
        if (nums1 == null || nums2 == null) {
            return null;
        }
        int nums1Length = nums1.length;
        int nums2Length = nums2.length;
        List<Integer> list = new ArrayList<>();
        for (int i = 0; i < nums1Length; i++) {
            for (int j = 0; j < nums2Length; j++) {
                if (nums1[i] == nums2[j] && !list.contains(nums1[i])) {
                    list.add(nums1[i]);
                    break;
                }
            }
        }
        int array[] = new int[list.size()];
        for (int i = 0; i < list.size(); i++) {
            array[i] = list.get(i);
        }
        return array;
    }
}
